import { h } from "vue";
import { NButton } from "naive-ui";
import { ColumnsConfig, ActionBtn } from "./type";

export const renderButton = (btn: ActionBtn, row: any, index: number) => {
    return h(
        NButton,
        {
            strong: true,
            quaternary: true,
            size: "small",
            type: "primary",
            onClick: () => btn.onClick(row, index),
        },
        { default: () => btn.text },
    );
};

export const beforeColumns = (col: ColumnsConfig) => {
    if (!col.render && col.formateHtml) {
        col.render = (row: any) => {
            return h("div", {
                innerHTML: col.formateHtml?.(row),
            });
        };
    }
    if (col.key == "actions" && col.actionsList.length && !col.render) {
        col.render = (row, index: any) => {
            return col.actionsList.map((btn) => {
                return renderButton(btn, row, index);
            });
        };
    }
};
